package hentai.utils;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Map;

public class RequestUtil {

    public static String getRequestParameters(HttpServletRequest req, HttpServletResponse resp) {
        StringBuffer sb = new StringBuffer();
        sb.append("_*_[" + req.getContextPath() + "] 收到 : <" + req.getMethod() + "> 请求 : \n");
        sb.append(req.getRequestURI().replaceFirst(req.getContextPath(), "") + "?" + req.getQueryString() + " 的全部参数:\n");
        Map<String, String[]> map = req.getParameterMap();
        map.forEach((k, v) -> {
            sb.append(k + " : ");
            sb.append(Arrays.deepToString(v));
            sb.append("\n");
        });
        return sb.toString();
    }

    public static void printRequestParameters(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println(getRequestParameters(req, resp));
    }

    public static void printRequestParameters(ServletRequest req, ServletResponse resp) {
        printRequestParameters((HttpServletRequest) req, (HttpServletResponse) resp);
    }
}
